<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\Admin\RankRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RankController extends Controller
{
    protected $rankRepository;
    public function __construct(RankRepository $rankRepository)
    {
        $this->rankRepository = $rankRepository;
    }
    /**
     * 获取用户等级列表
     */
    public function getUserRank(){
        $userRanks = $this->rankRepository->getByType(1);
        if($userRanks){
            return json_encode(['code'=>200,'userRanks'=>$userRanks]);
        }else{
            return json_encode(['code'=>400,'msg'=>'获取用户等级信息出错']);
        }
    }
    /**
     * 获取图片等级列表
     */
    public function getPictureRank(){
        $pictureRanks = $this->rankRepository->getByType(2);
        if($pictureRanks){
            return json_encode(['code'=>200,'pictureRanks'=>$pictureRanks]);
        }else{
            return json_encode(['code'=>400,'msg'=>'获取用户等级信息出错']);
        }
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if($request->has('rank_data')){
            $rankData = json_decode($request->post('rank_data'),true);
            $rank = [
                'rank_number' => $rankData['rank_number'],
                'rank_thumbnail' => $rankData['rank_thumbnail'],
                'rank_name' => $rankData['rank_name'],
                'rank_type' => $rankData['rank_type'],
                'rank_status' => $rankData['rank_status'],
                'rank_health_point' => (int)$rankData['rank_health_point'],
                'rank_prop_id' => (int)$rankData['rank_prop_id'],
                'rank_condition_type' => $rankData['rank_condition_type'],
                'rank_condition' => $rankData['rank_condition']
            ];
            $result = $this->rankRepository->createRank($rank);
            if($request)
                return json_encode(['code'=>200,'rank_id'=>$result->rank_id]);
            else
                return json_encode(['code'=>400,'msg'=>'保存等级数据时出错']);
        }else{
            return json_encode(['code'=>400,'msg'=>'没有传入相应的等级数据']);
        }
    }

    public function update(Request $request)
    {
//        return $request->toArray();
        if($request->has('updatd_rank_data')){
            $updatdRankData = json_decode($request->post('updatd_rank_data'),true);
            $rankId = $updatdRankData['rank_id'];
            $updateRank = [];
            if(array_key_exists('rank_thumbnail',$updatdRankData))
                $updateRank['rank_thumbnail'] = $updatdRankData['rank_thumbnail'];
            if(array_key_exists('rank_number',$updatdRankData))
                $updateRank['rank_number'] = $updatdRankData['rank_number'];
            if(array_key_exists('rank_name',$updatdRankData))
                $updateRank['rank_name'] = $updatdRankData['rank_name'];
            if(array_key_exists('rank_prop_id',$updatdRankData))
                $updateRank['rank_prop_id'] = $updatdRankData['rank_prop_id'];
            if(array_key_exists('rank_condition_type',$updatdRankData))
                $updateRank['rank_condition_type'] = $updatdRankData['rank_condition_type'];
            if(array_key_exists('rank_condition',$updatdRankData))
                $updateRank['rank_condition'] = $updatdRankData['rank_condition'];
            if(array_key_exists('rank_status',$updatdRankData))
                $updateRank['rank_status'] = $updatdRankData['rank_status'];
            if(array_key_exists('rank_health_point',$updatdRankData))
                $updateRank['rank_health_point'] = $updatdRankData['rank_health_point'];

            $result = $this->rankRepository->update($rankId,$updateRank);
            if($result)
                return json_encode(['code'=>200,'msg'=>'更新成功']);
            else
                return json_encode(['code'=>400,'msg'=>'更新等级数据时出错']);
        }else{
            return json_encode(['code'=>400,'msg'=>'没有传入相应的等级数据']);
        }
    }

    public function destroy(Request $request)
    {
        if($request->has('rank_id')) {
            $rankId = $request->post('rank_id');
            $result = $this->rankRepository->delete($rankId);
            if($result)
                return json_encode(['code'=>200,'msg'=>'删除成功']);
            else
                return json_encode(['code'=>400,'msg'=>'删除等级数据时出错']);
        }else{
            return json_encode(['code'=>400,'msg'=>'没有传入等级的id']);
        }
    }
}
